<?php

namespace App\Http\Resources\Counter;

use App\Enums\OrderItemType;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;

class OrderCollection extends ResourceCollection
{
    /**
     * Transform the resource collection into an array.
     *
     * @return array<int|string, mixed>
     */
    public function toArray(Request $request): array
    {
        $data = parent::toArray($request);

        foreach ($data as &$datum) {

            $datum['table_item'] = null;
            foreach ($datum['items'] as $item) {
                if ($item['type'] == OrderItemType::Table->value || $item['type'] == OrderItemType::Table)
                    $datum['table_item'] = $item;
            }
        }

        return $data;
    }
}
